// 导入axios
import axios from 'axios';
// 导入user
import { useUserStore } from "@/store/user";
import { ElNotification } from 'element-plus';

// 创建axios对象
const service = axios.create({
    // 超时设置
    timeout: 3000,
});

// 请求拦截器
service.interceptors.request.use(config => {
    const userStore = useUserStore();
    if (userStore.token) {
        config.headers.set("Authorization", userStore.token, false);
    }
    return config;
}, error => {
    return Promise.reject(error);
});

// 响应拦截器
service.interceptors.response.use(response => {
    return response.data;
}, error => {
    if (error.code == "ERR_NETWORK") {
        ElNotification({
            title: error.code,
            message: '加载失败，服务器网络异常',
            type: 'error',
        })
    }
    return Promise.reject(error);
});

export default service;